<!DOCTYPE html>
<html>
    <head><title>Input Test</title></head>
    <body>
        <br><br>

        Canvas:
        <span id="button-selection" style="display: none;">
            <input id="button1" type="button" value="L"><input id="button2" type="button" value="M"><input id="button4" type="button" value="R">
        </span>
        <br>
        <canvas id="canvas" width="640" height="20"
                style="border-style: dotted; border-width: 1px;">
            Canvas not supported.
        </canvas>

        <br>
        Results:<br>
        <textarea id="messages" style="font-size: 9;" cols=80 rows=25></textarea>
    </body>

    <!--
    <script type='text/javascript'
        src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
    -->
    <script src="../core/util.js"></script>
    <script src="../app/webutil.js"></script>
    <script src="../core/base64.js"></script>
    <script src="../core/input/keysym.js"></script>
    <script src="../core/input/keysymdef.js"></script> 
    <script src="../core/input/xtscancodes.js"></script>
    <script src="../core/input/util.js"></script>
    <script src="../core/input/devices.js"></script>
    <script src="../core/display.js"></script>
    <script>
        var msg_cnt = 0, iterations,
            width = 400, height = 200,
            canvas, keyboard, mouse;

        var newline = "\n";
        if (Util.Engine.trident) {
            var newline = "<br>\n";
        }

        function message(str) {
            console.log(str);
            cell = document.getElementById('messages');
            cell.textContent += msg_cnt + ": " + str + newline;
            cell.scrollTop = cell.scrollHeight;
            msg_cnt++;
        }

        function mouseButton(x, y, down, bmask) {
            msg = 'mouse x,y: ' + x + ',' + y + '  down: ' + down;
            msg += ' bmask: ' + bmask;
            message(msg);
        }

        function mouseMove(x, y) {
            msg = 'mouse x,y: ' + x + ',' + y;
            //console.log(msg);
        }

        function rfbKeyPress(keysym, down) {
            var d = down ? "down" : " up ";
            var key = keysyms.lookup(keysym);
            var msg = "RFB keypress " + d + " keysym: " + keysym;
            if (key && key.keyname) {
                msg += " key name: " + key.keyname;
            }
            message(msg);
        }
        function rawKey(e) {
            msg = "raw key event " + e.type +
                  " (key: " + e.keyCode + ", char: " + e.charCode +
                  ", which: " + e.which +")";
            message(msg);
        }

        function selectButton(num) {
            var b, blist = [1,2,4];

            if (typeof num === 'undefined') {
                // Show the default
                num = mouse.get_touchButton();
            } else if (num === mouse.get_touchButton()) {
                // Set all buttons off (no clicks)
                mouse.set_touchButton(0);
                num = 0;
            } else {
                // Turn on one button
                mouse.set_touchButton(num);
            }

            for (b = 0; b < blist.length; b++) {
                if (blist[b] === num) {
                    document.getElementById('button' + blist[b]).style.backgroundColor = "black";
                    document.getElementById('button' + blist[b]).style.color = "lightgray";
                } else {
                    document.getElementById('button' + blist[b]).style.backgroundColor = "";
                    document.getElementById('button' + blist[b]).style.color = "";
                }
            }
        }

        window.onload = function() {
            canvas = new Display({'target' : document.getElementById('canvas')});
            keyboard = new Keyboard({'target': document,
                                    'onKeyPress': rfbKeyPress});
            document.addEventListener('keypress', rawKey);
            document.addEventListener('keydown', rawKey);
            document.addEventListener('keyup', rawKey);
            mouse    = new Mouse({'target': document.getElementById('canvas'),
                                'onMouseButton': mouseButton,
                                'onMouseMove': mouseMove});

            canvas.resize(width, height, true);
            keyboard.grab();
            mouse.grab();
            message("Display initialized");

            if (Util.isTouchDevice) {
                message("Touch device detected");
                document.getElementById('button-selection').style.display = "inline";
                document.getElementById('button1').onclick = function(){ selectButton(1) };
                document.getElementById('button2').onclick = function(){ selectButton(2) };
                document.getElementById('button4').onclick = function(){ selectButton(4) };
                selectButton();
            }

        }
    </script>
</html>
